package com.hyacinth.core.sysmgr.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.hyacinth.common.framework.ControllerSupport;
import com.hyacinth.common.framework.i.IServiceSupport;
import com.hyacinth.core.sysmgr.domain.SysMenu;
import com.hyacinth.core.sysmgr.service.IMenuService;
import com.hyacinth.web.ui.easyui.ComboTree;
import com.hyacinth.web.ui.easyui.TreeGrid;

@Controller
@RequestMapping("/system/menu/*")
public class MenuController extends ControllerSupport<SysMenu> {

	private static final long serialVersionUID = 292563402710445509L;
	
	private IMenuService menuService;
	
	@Override
	public String[] getFields() {
		String[] fields = {"name","id","url","sort","icon","pid","children"};
		return fields;
	}
	
	@Override
	public IServiceSupport<SysMenu> getService() {
		return this.menuService;
	}
	
	@RequestMapping("listTree.do")
	public @ResponseBody String listTree(@RequestParam int type, @RequestParam long id) throws Exception {
		System.out.println("ddddd");
		List<SysMenu> menus = menuService.all();
		if(type == 1) {
			return TreeGrid.getData2(menus.get(0).getChildren(), SysMenu.class, getFields());
		}else if(type == 2) {
			return ComboTree.getData(menus, SysMenu.class, getFields());
		}else{
			return "[]";
		}
	}
	
	@RequestMapping("enter.do")
	public String enter() throws Exception {
		return "/system/menu/list";
	}
	
	@RequestMapping("edit.do")
	public ModelAndView edit(@RequestParam Long id) throws Exception {
		if(id!=null) {
			Map<String,SysMenu> map = new HashMap<String, SysMenu>();
			SysMenu m = menuService.find(id);
			map.put("domain", m);
			return new ModelAndView("/system/menu/edit",map);
		}
		return new ModelAndView("/system/menu/edit");
	}

	public IMenuService getMenuService() {
		return menuService;
	}

	@Resource
	public void setMenuService(IMenuService menuService) {
		this.menuService = menuService;
	}
}
